package pile;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private DrawEnvironnement paint;
	private static Environnement environnement; 
	private static String code;
	private static JTextArea affiche;
	private static  JComboBox<String> type = new JComboBox<String>();
	private static  JComboBox<String> vars1 = new JComboBox<String>();
	private static  JComboBox<String> vars2 = new JComboBox<String>();
	
	public GUI() throws HeadlessException {
		//super();
		this.setTitle("Gestion dynamique d'une pile d'execution");
		this.setSize(600,600);
		Container content = this.getContentPane();
	    content.setBackground(Color.WHITE);
	    content.setLayout(new BorderLayout()); 
		affiche = new JTextArea(30, 15);
	    
		type.addItem("");
		type.addItem("int");
		type.addItem("char");
		
		JButton create = new JButton("create");
		//create.setBounds(10, 10, 220, 30);
		create.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(!type.getSelectedItem().equals("")){
					environnement.createVar((String)type.getSelectedItem());
					vars1.removeAllItems();
					vars2.removeAllItems();
					ArrayList<String> varnames = environnement.getVarNames();
					for(int i=0; i<varnames.size(); i++){
						vars1.addItem(varnames.get(i));
						vars2.addItem(varnames.get(i));
					}
					// int placemem = environnement.vars.get(environnement.vars.size()-1).placeMemoire;
					String valstring = environnement.memoire.get(environnement.memoire.size()-1).valeur;
					affiche.append((String)type.getSelectedItem()+" "+varnames.get(varnames.size()-1)+" = "+valstring+" ;\n");
					getPaint().repaint();
				}
			}
		});
		
		JButton egalite = new JButton("egal");
		egalite.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				VariablePile varpile1 = environnement.getVarPile((String)vars1.getSelectedItem());
				VariablePile varpile2 = environnement.getVarPile((String)vars2.getSelectedItem());
				environnement.equalVar(varpile1, varpile2);
				getPaint().repaint();
				affiche.append((String)vars1.getSelectedItem()+" = "+(String)vars2.getSelectedItem()+" ;\n");
			}
			//this.getPaint().repaint();		
		});
		
		//JPanel panel = new JPanel();
		//panel.setBackground(Color.GREEN);
		//this.setContentPane(panel);
		JPanel pandown = new JPanel();
		pandown.setLayout(new GridLayout(1,3));
		pandown.add(vars1);
		pandown.add(vars2);
		pandown.add(egalite);
		JPanel panup = new JPanel();
		panup.setLayout(new GridLayout(1,2));
		panup.add(type);
		panup.add(create);
		
		content.add(panup, BorderLayout.NORTH);
		content.add(pandown, BorderLayout.SOUTH);
		
		environnement = new Environnement();
		paint = new DrawEnvironnement(environnement);
		//JPanel jp = new JPanel();
		//jp.setPreferredSize(new Dimension(300, 300));		
		content.add(paint,BorderLayout.WEST);
		content.add(affiche,BorderLayout.EAST);
		//add("Center", jp);
		
		this.pack();
		this.setVisible(true);
	}
	
	public DrawEnvironnement getPaint() {
		return paint;
	}

	public void setPaint(DrawEnvironnement paint) {
		this.paint = paint;
	}
	
	public static void main(String[] args) throws InterruptedException {
		GUI gui = new GUI();
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui.setVisible(true);
		/*Thread.sleep(1000);
		while(true){
			gui.getPaint().repaint();
		}*/
	}
}
